import analytics from '@react-native-firebase/analytics';

import { localStorage } from './storage';
import { USER_ID } from '@/constants';

/**
 * firebase analytics 事件
 * @description 开发文档
 * @see https://rnfirebase.io/analytics/usage react native firebase analytics 开发文档
 */

const baseParams = () => {
  return {
    app_user_id: localStorage.getStorageItem(USER_ID),
  };
};

export const FirebaseEventsUtil = {
  // 启动页
  launch: (index: number) => {
    analytics()
      .logEvent('ts_boot_page_click_next', {
        ...baseParams(),
        route_from: `welcome_${index}`,
      })
      .then(() => {
        console.log('启动页', index);
      });
  },
  /** 订阅页中，用户点击了购买，拉取付费系统弹框（不需要支付成功） */
  click_subscription: (plan: string) => {
    analytics().logEvent('ts_subscription_click_checkout', {
      ...baseParams(),
      common_info: plan,
    });
  },
  /** 订阅页中，用户点击了购买，拉取付费系统弹框（不需要支付成功）,点击了关闭弹框*/
  cancel_subscription: (plan: string) => {
    analytics().logEvent('ts_subscription_click_checkout_cancel', {
      ...baseParams(),
      common_info: plan,
    });
  },
  /** 订阅页中，用户点击了关闭页面按钮 */
  close_subscription: () => {
    analytics().logEvent('ts_subscription_click_close_page', {
      ...baseParams(),
    });
  },
  /** 订阅成功 */
  success_subscription: (plan: string) => {
    analytics().logEvent('ts_subscription_click_checkout_success', {
      ...baseParams(),
      common_info: plan,
    });
  },
  /**
   * 前往订阅页面
   * @param form 从哪个页面跳转过来
   * @param subscriptionPage: 订阅页面
   */
  goto_subscription: (form: string, subscriptionPage?: string) => {
    analytics().logEvent('ts_subscription_access_page', {
      ...baseParams(),
      route_from: form,
      common_info: subscriptionPage,
    });
  },
  /** 进入功能模块 */
  enter_module: (module: string) => {
    analytics().logEvent('ts_home_click_first_interest_module', {
      ...baseParams(),
      enter_type: module,
    });
  },
  /** 使用功能 */
  use_module: (module: string) => {
    analytics().logEvent('ts_create_do_action', {
      ...baseParams(),
      style_type: module,
    });
  },
  /** 页面停留时长 */
  page_stay_time: (page: string, time: number) => {
    analytics().logEvent('ts_page_stay_time', {
      ...baseParams(),
      page: page + time,
      common_info: time,
    });
  },
  /** 语音转文字功能是否成功 */
  voice_to_text: (status: string, time: number) => {
    console.log('voice_to_text', status, time);

    analytics().logEvent('ts_feature_tts_status', {
      ...baseParams(),
      common_info: status,
      time: time,
    });
  },
};
